//
//  STISystemInfo.m
//  vogue
//
//  Created by QFish on 12/10/14.
//  Copyright (c) 2014 geekzoo. All rights reserved.
//

#import "STISystemInfo.h"
#import "SSKeychain.h"

@interface STISystemInfo ()
@end

@implementation STISystemInfo

+ (BOOL)everLaunched
{
	return [self launchedTimes] > 0;
}

+ (NSInteger)launchedTimes
{
	NSInteger launchedTimes = [[NSUserDefaults standardUserDefaults] integerForKey:@"app.launched.times"];
	
	static dispatch_once_t onceToken;
	dispatch_once(&onceToken, ^{
		[[NSUserDefaults standardUserDefaults] setInteger:(launchedTimes + 1)
												   forKey:@"app.launched.times"];
	});
	
	return launchedTimes;
}

+ (NSString *)appVersion
{
	static NSString * __identifier = nil;
	if ( nil == __identifier )
	{
		__identifier = [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] copy];
	}
	return __identifier;
}

+ (NSString *)appIdentifier
{
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
    static NSString * __identifier = nil;
    if ( nil == __identifier )
    {
        __identifier = [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"] copy];
    }
    return __identifier;
#else	// #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
    return @"";
#endif	// #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
}

+ (NSString *)deviceUUID
{
    static NSString * __udid = nil;
    if ( nil == __udid )
    {
        NSString * udid = [SSKeychain passwordForService:[self appIdentifier] account:@"udid"];
        if (!udid) {
            udid = [[UIDevice currentDevice].identifierForVendor UUIDString];
            [SSKeychain setPassword:udid forService:[self appIdentifier] account:@"udid"];
        }
        __udid = udid;
    }
    return __udid;
}

@end
